package com.linzhaowei.headhuntingservice;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.linzhaowei.headhuntingservice.bean.Delivery;
import com.linzhaowei.headhuntingservice.bean.JobObjective;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * 简历投递-选择意向界面
 */
public class Deliver extends AppCompatActivity implements View.OnClickListener {

    List<JobObjective> jobObjectiveList = new ArrayList<>();

    RadioGroup radioGroup;
    Button button;

    Delivery delivery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deliver);

        delivery = (Delivery) getIntent().getSerializableExtra("delivery");

        initViews();

        setEvents();

        getJobObjective();
    }

    private void initViews() {
        radioGroup = findViewById(R.id.d_chose);
        button = findViewById(R.id.d_btn);
    }

    private void setEvents() {
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.d_btn:
                deliver();
                break;
        }
    }


    void getJobObjective() {
        new Thread(() -> {
            HttpUtils httpUtils = new HttpUtils();
            try {
                SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                String result = httpUtils.get(Ip.ip + "/user/job_objective_by_user?user_id=" + sharedPreferences.getInt("id", -1));
                JSONArray jsonArray = JSON.parseArray(result);
                for (int i = 0; i < jsonArray.size(); i++) {
                    jobObjectiveList.add(jsonArray
                            .getJSONObject(i)
                            .toJavaObject(JobObjective.class));
                }

                runOnUiThread(this::setRadioGroup);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    void setRadioGroup() {
        for (int i = 0; i < jobObjectiveList.size(); i++) {
            RadioButton radioButton = new RadioButton(this);
            radioButton.setPadding(50, 0, 0, 0);
            String text = jobObjectiveList.get(i).getName() + "  " + jobObjectiveList.get(i).getAddress();
            radioButton.setText(text);
            radioButton.setTextSize(24);
            radioGroup.addView(radioButton, MATCH_PARENT, WRAP_CONTENT);
        }
    }

    void deliver() {
        RadioButton radioButton;
        radioButton = findViewById(radioGroup.getCheckedRadioButtonId());
        int i = radioGroup.indexOfChild(radioButton);
        delivery.setJobObjectiveId(String.valueOf(jobObjectiveList.get(i).getId()));
        String string = JSON.toJSONString(delivery);

        HttpUtils httpUtils = new HttpUtils();

        new Thread() {
            @Override
            public void run() {
                try {
                    httpUtils.post(Ip.ip + "/user/addDelivery", string);
                    runOnUiThread(() -> Toast.makeText(Deliver.this, "成功", Toast.LENGTH_SHORT).show());
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();


    }

}
